﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Defect_Tracking_System.Helpers
{
    public class PaginatedList<T> : List<T>
    {
        public int PageSize { get; private set; }
        public int PageIndex { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }

        public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
        {
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
            TotalPages = TotalPages > 0 ? TotalPages : 1;
            //PageIndex = pageIndex > TotalPages ? TotalPages : pageIndex;
            if (pageIndex != 0 && pageIndex > TotalPages)
                pageIndex = TotalPages;
            else PageIndex = pageIndex;
            this.AddRange(source.Skip((PageIndex - 1) * PageSize).Take(PageSize));
        }

        public bool hasNext
        {
            get { return PageIndex < TotalPages; }
        }

        public bool hasPrevious
        {
            get { return PageIndex > 1; }
        }
    }
}